<?php
class M_ACL_User extends MVC_Model_Base implements ACL_User_Interface {

    private $_arData = array(
        'id' => -1,
        'login' => '@guest',
        'mail' => null,
        'password' => null,
        'first_name' => null,
        'last_name' => null,
        'middle_name' => null,
        'sex' => null,
        'birthday' => null,
        'photo' => null
    );


    private $_objAccess = null;


    public function athorization (ACL_AthorizationData_Interface $objData) {
        if ($this->_arData['id'] === -1) {
            $arAccount = $this->_getAccount($objData);
            if (!empty($arAccount)) {
                $this->_arData = $arAccount;
                return true;
            }
        }
        return false;
    }


    public function getId () {
        return $this->_arData['id'];
    }


    public function getLogin () {
        return $this->_arData['login'];
    }


    public function getMail () {
        return $this->_arData['mail'];
    }


    public function getFirstName () {
        return $this->_arData['first_name'];
    }


    public function getLastName () {
        return $this->_arData['last_name'];
    }


    public function getMiddleName () {
        return $this->_arData['middle_name'];
    }


    public function getSex () {
        return $this->_arData['sex'];
    }


    public function getBirthday () {
        return $this->_arData['birthday'];
    }


    public function getPhoto () {
        return $this->_arData['photo'];
    }


    public function getAccess() {
        if (empty($this->_objAccess)) {
            $this->_objAccess = $this->getModel('ACL_Access');
        }
        return $this->_objAccess;
    }


    private function _getAccount (ACL_AthorizationData_Interface $objData) {
        $Accounts = $this->getDbReader('MAIN', 'Accounts');
        $Accounts->elementBy(
            '(login = ? OR mail = ?) AND password = MD5(?)',
            $objData->getLogin(),
            $objData->getLogin(),
            $objData->getPassword()
        );
    }
}